﻿using FineUI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ZHDJ.Core.Caching;
using ZHDJ.Data;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Other
{
    public partial class CameraBranchNavigation : System.Web.UI.Page
    {
        public string Json
        {
            get
            {
                return Request["Json"] ?? "";
            }
        }
        public string Search
        {
            get
            {
                return Request["Search"] ?? "";
            }
        }

        public string BranchID
        {
            get
            {
                return Request["branchid"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        #region 数据操作

        private void LoadData()
        {
            if (Json != "" && Search == "")
            {
                BindTree();
            }
            else if (Json != "" && Search != "")
            {
                BindTreeBySearch();
            }
        }

        private void BindTree()
        {
            TreeControlBusiness tree = new TreeControlBusiness("BranchID", "ParentBranchID", "BranchName",
                true, CacheKeys.Cache_Branch_Camera_Tree_Navigation);
            tree.CommandName = "BranchType";

            var TreeBranch = new FineUI.Tree();
            if (BranchID == "")
            {
                tree.BindTreeCameraBranch(TreeBranch);
            }
            else
            {
                tree.BindTreeCameraBranch(TreeBranch,BranchID);
            }

            List<TreeData> jqtree = new List<TreeData>();
            foreach (var item in TreeBranch.Nodes)
            {
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = 0,iscamera = item.CommandName };
                jqtree.Add(data);
                CreateTreeData(item.Nodes, data);
            }
            var json = JsonConvert.SerializeObject(jqtree);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }

        /// <summary>
        /// 绑定查询
        /// </summary>
        private void BindTreeBySearch()
        {
            using (var DB = new DBInstance().DB)
            {
                List<TreeData> jqtree = new List<TreeData>();
                var data = DB.PartyOrgBranchs.Where(a => a.BranchName.Contains(Search)).OrderBy(a => a.BranchName).ToList();

                data.ForEach(a =>
                {
                    jqtree.Add(new TreeData() { id = a.BranchID, name = a.BranchName, level = 1 });
                });
                var json = JsonConvert.SerializeObject(jqtree);
                Response.Clear();
                Response.Write(json);
                Response.End();
            }
        }

        public void CreateTreeData(TreeNodeCollection treeNodes, TreeData treeData)
        {
            if (treeNodes == null)
            {
                return;
            }

            foreach (var item in treeNodes)
            {
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = treeData.level + 1,iscamera = item.CommandName };
                treeData.children.Add(data);
                CreateTreeData(item.Nodes, data);
            }
        }
        #endregion
    }

    public class TreeData
    {
        public TreeData()
        {
            id = "";
            name = "";
            children = new List<TreeData>();
            level = 0;
        }

        public int level { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public List<TreeData> children { get; set; }
        public string iscamera { get; set; }
    }
}